var id_card_check = "false";

function checkUser(){
    hideAlertMessage();
    var userObj = registerObj();
    
    // check empty
    var formElem = document.getElementById("register_form");
    var inputList = formElem.getElementsByTagName("input");
    for (var i = 0; i < inputList.length; i++) {
        var elem = inputList[i];
        if(elem.value == ""){
            var elemName = elem.getAttribute("name");
            var msg = elemName + " can not be empty.";
            showAlertMessage(msg);
            return false;
        }
    }
    
    // check username
    if(userObj.username.length <= 3 || userObj.username.length >= 20){
        var msg = "username length should > 3 and <20";
        showAlertMessage(msg);
        return false;
    }
    
    // check password & repassword
    if(!(userObj.password.indexOf(userObj.repassword) != -1 && userObj.repassword.indexOf(userObj.password) != -1)){
        var msg = "repassword is not same as password.";
        // var msg = userObj.password + " : " + userObj.repassword;
        showAlertMessage(msg);
        return false;
    }
    
    // check password
    if(userObj.password.length <= 3 || userObj.password.length >= 20){
        var msg = "password length should > 3 and <20";
        showAlertMessage(msg);
        return false;
    }
    
    // check id card
    // need Ajax
    if(userObj.idcard.length != 18){
        var msg = "ID card is not valid.";
        showAlertMessage(msg);
        id_card_check = "false";
        return false;
    }
    
    ajaxDoc(userObj.idcard);
    if(id_card_check.indexOf("true") != -1){
        var msg = "ID card is already in use.";
        showAlertMessage(msg);        
        return false;
    }
    
    // check phone
    if(userObj.phone.length != 11){
        var msg = "Phone is not valid.";
        showAlertMessage(msg);
        return false;
    }
    
    return true;
}

function showAlertMessage(msg){
    var alertElem = document.getElementById("alertMessage");
    alertElem.innerText = msg;
    alertElem.style.display = "block";
}

function hideAlertMessage(){
    var alertElem = document.getElementById("alertMessage");
    alertElem.style.display = "none";
}

function registerObj(){
    
    var username = document.getElementById("register_username").value;
    var password = document.getElementById("register_password").value;
    var repassword = document.getElementById("register_repassword").value;
    var idcard = document.getElementById("register_idcard").value;
    var phone = document.getElementById("register_phone").value;
    
    var obj = {
        username : username,
        password : password,
        repassword : repassword,
        idcard : idcard,
        phone : phone
    }
    
    return obj;
    
}

function ajaxDoc(idcard){
    xmlhttp = new XMLHttpRequest();
    
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            var text = xmlhttp.responseText;
            id_card_check = text;
        }
    }
    
    xmlhttp.open("POST", "./background/register_ajax.jsp", false);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    var send_text = "id_card=" + idcard;
    xmlhttp.send(send_text);
    
    return false;
}
